package com.tangdi.production.mpcctp.controller;

import com.tangdi.production.mpbase.util.HttpUtil;
import com.tangdi.production.mpbase.util.MD5;
import com.tangdi.production.mpbase.util.MapUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
public class MockController {

    private static final Logger log = LoggerFactory.getLogger(MockController.class);

    public static String ORG_NO = "8200600266";
    public static String custId = "20061600001889";
    public static String SIGNKEY = "C8DBD28C7BF2A080F7A4145A30CE0B8C";

    //    	public static String url = "http://154.209.249.72:8083/mpcctp/";// 服务器地址
    public static String url = "http://192.168.144.103:8083/mpcctp/";// 服务器地址

    @RequestMapping("wx-h5")
    public String wxH5() throws Exception {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        String action = "cashier/pay.ac";// 下单或支付;
        paramMap.put("version", "2.1");
        paramMap.put("orgNo", ORG_NO);
        paramMap.put("custId", custId);
        paramMap.put("custOrderNo", DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.randomAlphabetic(7));
        paramMap.put("payAmt", "101");// 交易金额 分
        paramMap.put("frontUrl", "http://www.baidu.com");
        paramMap.put("tranType", "0406");
        paramMap.put("sign", sign(paramMap,SIGNKEY));
        System.out.println("url:" + url + action);

        Map<String,Object> requestHead = new LinkedHashMap<>();
        requestHead.put("x-forwarded-for", "120.24.9.5,120.24.9.6");
        requestHead.put("Proxy-Client-IP", "120.24.9.5,120.24.9.6");
        requestHead.put("WL-Proxy-Client-IP", "120.24.9.5,120.24.9.6");
        requestHead.put("HTTP_CLIENT_IP", "120.24.9.5,120.24.9.6");
        requestHead.put("HTTP_X_FORWARDED_FOR", "120.24.9.5,120.24.9.6");
        String resStr = HttpUtil.httpRequest(url + action, paramMap, "POST", "UTF-8", requestHead);
        System.out.println(resStr);

        return "";

    }

    public static String sign(Map<String,Object> paramMap,String signKey){
        String signStr = MapUtils.getSignStrByTreeMap(paramMap, true);
        log.info("签名元数据:[{}]",signStr);
        signStr += "&key=" + signKey;
        String signValue = MD5.encryption(signStr).toUpperCase();
//		String signValue = MD5Util.digest(signStr).toUpperCase();
        log.info("签名前完整字符串:[{}] 签名结果[{}]",signStr,signValue);
        return signValue;
    }

}
